package cn.dt.org.service.impl;

import cn.dt.basic.exception.BusinessException;
import cn.dt.basic.util.BaiduAuditUtils;
import cn.dt.basic.util.MailUtil;
import cn.dt.org.domain.Employee;
import cn.dt.org.domain.Shop;
import cn.dt.org.domain.ShopAuditLog;
import cn.dt.org.mapper.EmployeeMapper;
import cn.dt.org.mapper.ShopAuditLogMapper;
import cn.dt.org.mapper.ShopMapper;
import cn.dt.org.service.IShopService;
import cn.dt.basic.service.impl.BaseServiceImpl;
import cn.dt.org.vo.ShopVo;
import org.omg.PortableServer.POAManagerPackage.AdapterInactive;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author daiwei
 * @since 2022-06-12
 */
@Service
public class ShopServiceImpl extends BaseServiceImpl<Shop> implements IShopService {
    @Autowired
    private ShopMapper shopMapper;
    @Autowired
    private EmployeeMapper employeeMapper;
    @Autowired
    private ShopAuditLogMapper shopAuditLogMapper;
    /**
     * 新增功能实现
     * 1.判断参数不为空，为空抛出异常
     * 2.敏感字筛选
     * 3.判断门店是否存在，存在抛出异常
     * 4.判断店长是否存在，存在并且拥有门店则抛出异常，如果店长不存在则创建并且要返回
     * @param shop
     */
    @Override
    public void settlement(Shop shop) {
        Employee admin = shop.getAdmin();
        //1.判断参数是否为空(如果为空抛出一个错误)
        if(StringUtils.isEmpty(shop.getName())
            ||StringUtils.isEmpty(shop.getAddress())
            ||StringUtils.isEmpty(admin.getUsername())    
        ){
            throw  new BusinessException("数据不能为空");
        }
        //2.智能筛选(敏感词汇排除，百度ai
        if(!BaiduAuditUtils.TextCensor(shop.getName())){
            throw new BusinessException("请文明用语");
        }
        if(!BaiduAuditUtils.ImgCensor("http://123.207.27.208"+shop.getLogo())){
            throw new BusinessException("请上传合法图片");
        }
        //3.判断门店是否注册，如果存在则抛出异常，
        Shop shopTmp = shopMapper.loadByName(shop.getName());
        if(shopTmp!=null){
            throw  new BusinessException("门店已经存在。");
        }
        //4.判断店长是否存在，如果存在并且包含门店则抛出异常
        Employee empTmp = employeeMapper.loadByUsername(admin.getUsername());
        if(empTmp!=null && empTmp.getShop_id()!=null){
            throw  new BusinessException("该员工已有门店，请再次确认信息是否正确。");
        }
        //5.如果不存在则创建店长这个员工，
        if(empTmp==null){
            employeeMapper.save(admin);
            empTmp = admin;
        }
        //新建员工以后修改shop_id
        shopMapper.save(shop);
        empTmp.setShop_id(shop.getId());
        employeeMapper.update(empTmp);
        shop.setAdminId(empTmp.getId());
        shopMapper.update(shop);




    }

    /**
     * 审核通过，
     * 1.修改状态，
     * 2。添加日志
     * @param log
     */
    @Override
    public void savePass(ShopAuditLog log) {
        //* 1.修改状态，
        Long shopId = log.getShopId();
        Shop shop = shopMapper.loadById(shopId);
        shop.setState(1);
        shopMapper.update(shop);
        log.setAuditId(334L);//@TODO
        //* 2。添加日志
        shopAuditLogMapper.save(log);
        //发送邮件提示激活成功
        Shop shop1 = shopMapper.loadById(log.getShopId());
        Employee employee = employeeMapper.loadById(shop1.getAdminId());
        MailUtil.sendSimpleMail(employee.getEmail(),"激活成功","http://localhost:8080/shop/activation?id="+shop.getId());

    }

    /**
     * 驳回
     * 1.
     * @param log
     */
    @Override
    public void reject(ShopAuditLog log) {
        //* 1.修改状态，
        Long shopId = log.getShopId();
        Shop shop = shopMapper.loadById(shopId);
        shop.setState(-1);
        shopMapper.update(shop);
        log.setAuditId(334L);//@TODO
        //* 2。添加日志
        shopAuditLogMapper.save(log);
        //3.发送邮件
//        System.out.println(log.getShopId());
        Shop shop1 = shopMapper.loadById(log.getShopId());
        Employee employee = employeeMapper.loadById(shop1.getAdminId());
//        SimpleMailMessage message = new SimpleMailMessage();
//        message.setFrom("1042998397@qq.com");
//        message.setTo(employee.getEmail());
//        message.setSubject("宠物乐园-店铺入驻驳回任务");
//        message.setText("亲，您的店铺信息有误，审核不通过，请尽快修改并重新提交审核！！修改链接：" +
//                "http://localhost:8081/#/register2?shopId="+log.getShopId());
//        javaMailSender.send(message);
        MailUtil.sendSimpleMail(employee.getEmail(),"宠物乐园-店铺入驻驳回任务","亲，您的店铺信息有误，审核不通过，请尽快修改并重新提交审核！！修改链接：http://localhost:8081/#/register2?Id="+log.getShopId());
    }

    @Override
    public void batchSave(List<Shop> shops) {
        shopMapper.batchSave(shops);
    }

    @Override
    public List<ShopVo> countByState() {
        return shopMapper.countByState();
    }

    @Override
    public void activation(Long id) {
        System.out.println(id);
        Shop shop = shopMapper.loadById(id);
        shop.setState(1);
        shopMapper.update(shop);

    }


}
